package com.liuke.study.thread;

import com.sun.corba.se.spi.orbutil.threadpool.Work;

import java.util.concurrent.locks.Lock;

/**
 * @author wangjwy
 * @date 2019/10/22 14:58
 * @since 1.0.0
 */
public class Test6 {

	/*
	创建两个线程，其中一个输出1-52，另外一个输出A-Z。输出格式要求：12A 34B 56C 78D
	*/
	public static void main(String[] args) throws InterruptedException {
		Object lockA = new Object();

		new Thread(new Work1(lockA)).start();
		Thread.sleep(100);
		new Thread(new WorkA(lockA)).start();
		Thread.sleep(10000);
	}

}

class Work1 implements Runnable {
	/**
	 * print 1-52
	 */

	private int count = 0;

	private Object lockA;


	public Work1(Object lockA) {
		this.lockA = lockA;
	}

	@Override
	public void run() {
		synchronized (lockA) {
			while (count <= 50) {
				System.out.print(++count);
				System.out.print(++count);
				try {
					lockA.notify();
					lockA.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			lockA.notify();
		}
	}
}

class WorkA implements Runnable {
	/**
	 * print 1-52
	 */

	private char count = 'A';

	private Object lockA;


	public WorkA(Object lockA) {
		this.lockA = lockA;
	}

	@Override
	public void run() {
		synchronized (lockA) {

			while (count <= 'Z') {
				System.out.print(count++);
				System.out.print(" ");
				try {
					lockA.notify();
					lockA.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			lockA.notify();
		}
	}
}